
package cn.xt.controller.com;

import cn.xt.bean.*;
import cn.xt.service.com.ComService;

import cn.xt.util.FileImageUtils;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/com")
public class ControlCom {
    @Autowired
    private ComService comService;

    //增加评论
@RequestMapping(value = "/addcom.html")
   public String addcom(Com com ,House house,@RequestParam("pl")String pl,
                        @RequestParam("house_id")int house_id,
                        @RequestParam("fileimg")MultipartFile attach,
                        HttpServletRequest request,
                        HttpSession session){
    //房屋id
    house.setHouse_id(house_id);
    com.setHouse(house);
    //用户id
    Users users=(Users) session.getAttribute("user");
    com.setUsers(users);
    //评论内容
    com.setPl(pl);
    //评论时间
    com.setPltime(new Date());
    //上传图片
    String idPicPath=FileImageUtils.ImageFile(request,attach);
    if(idPicPath!=null && idPicPath!=""){
        com.setCom_img(idPicPath);
    }
    //默认未读
    com.setRead(0);
    comService.addcom(com);
    return "ReviewHG";
}
//查看我的评论
@RequestMapping("/comlist.html")
    public String comlist(HttpSession session, Model model){
    // session 中获取users
    Users users =(Users) session.getAttribute("user");
    List<Com> comlist=comService.comlist(users.getUsers_id());
    Fimg fimg=null;
    //遍历comlist获取house_id
    for (Com com :comlist) {

        fimg=comService.fimg(com.getHouse().getHouse_id());
    }
    //fimg 存入model
    model.addAttribute("img",fimg);
    //comlist存入model
    model.addAttribute("comlist",comlist);
    return "MyRating";
}
//查看房东可回复的评论
@RequestMapping("/sjhousecom.html")
    public String sjhousecom(Model model,HttpSession session){
    // session 中获取users
    Users users =(Users) session.getAttribute("user");
    List<Com> coms=comService.sjhousecom(users.getUsers_id());
    Fimg fimg=null;
    //遍历coms获取house_id
    for (Com com :coms){
        fimg=comService.fimg(com.getHouse().getHouse_id());
    }
    //coms 存入model
    model.addAttribute("coms",coms);
    //fimg 存入model
    model.addAttribute("img",fimg);
    return "UserNews";
}
//房东回复房客评论
@RequestMapping("/addhfcom.html")
    public String addhfcom(Hfcom hfcom, Model model,
                           Users users,Com com,HttpSession session,
                           @RequestParam("com_id")int com_id,
                           @RequestParam("hfplcom")String hfplcom){
        // session 中获取users
        users=(Users) session.getAttribute("user");
        //users
        hfcom.setUsers(users);
        //hfplcom
        hfcom.setHfplcom(hfplcom);
        //comlist
        List<Com> comlist=comService.comlist(users.getUsers_id());
       //遍历comlist获取评论id
        for (Com com1 :comlist){
            com.setCom_id(com1.getCom_id());
        }
        //hf.plcom
        hfcom.setPlcom(com);
        //addhfcom
        comService.addhfcom(hfcom);
        session.setAttribute("hfcom" ,hfcom);
        comService.updRead(com_id);
        return "redirect:/com/sjhousecom.html";
}

    //收藏
    @RequestMapping("/addSc.json")
    @ResponseBody
    public Object updSc(HttpSession session,Collectionhouse collectionhouse,Users user,House house,@RequestParam("houseId")int houseId){
        Users users=(Users) session.getAttribute("user");
        user.setUsers_id(users.getUsers_id());
        house.setHouse_id(houseId);
        collectionhouse.setHouse(house);
        collectionhouse.setUsers(users);
        HashMap<String,String> jesn=new HashMap<String, String>();
        int i = comService.addcollocation(collectionhouse);
        if (i>0){
            jesn.put("ok","true");
        }
       return JSONArray.toJSONString(jesn);
    }
    //取消收藏1
    @RequestMapping("updSc1.json")
    @ResponseBody
    public Object updcoll1(HttpSession session,@RequestParam("houseId")int houseId,Model model){
        System.out.println("收藏");
        Users users=(Users) session.getAttribute("user");
        HashMap<String,String> jesn=new HashMap<String, String>();
        Collectionhouse collectionhouse=comService.collection(users.getUsers_id(),houseId);
        model.addAttribute("collection",collectionhouse);
    int i = comService.updcoll1(houseId,users.getUsers_id());
        if (i>0){
            jesn.put("ok","true");
        }
        return JSONArray.toJSONString(jesn);
    }
    //取消收藏2
    @RequestMapping("updSc2.json")
    @ResponseBody
    public Object updcoll2(HttpSession session,@RequestParam("houseId")int houseId,Model model){
        System.out.println("取消收藏");
        Users users=(Users) session.getAttribute("user");
        HashMap<String,String> jesn=new HashMap<String, String>();
        Collectionhouse collectionhouse=comService.collection(users.getUsers_id(),houseId);
        model.addAttribute("collection",collectionhouse);
        int i = comService.updcoll2(houseId,users.getUsers_id());
        if (i>0){
            jesn.put("ok","true");
        }
        return JSONArray.toJSONString(jesn);
    }

    @RequestMapping( value = "/mycollection.html")
    public String mycoll(HttpSession session,Model model){
    Users users=(Users) session.getAttribute("user");
    List<Collectionhouse> collectionhouses=comService.mycolllist(users.getUsers_id(),1);
    for (Collectionhouse collectionhouse:collectionhouses){
        System.out.println(collectionhouse.getHouse().getTitle());
        }
    model.addAttribute("mycoll",collectionhouses);
    return "collection";
    }
    @RequestMapping(value = "/updcoll.html/{collection_id}")
    public String updcoll(@PathVariable("collection_id")int collection_id,HttpSession session){
    Users users=(Users)session.getAttribute("user");
    comService.updcoll(collection_id,users.getUsers_id());
        System.out.println(collection_id);
        return "redirect:/com/mycollection.html";
    }

    //跳转 /assess.html/{house_id}
    @RequestMapping(value = "/assess.html/{house_id}")
    public String assess(/*传参house_id*/@PathVariable("house_id") Integer house_id, Model model){
    //存house_id
    model.addAttribute("house_id",house_id);
        return "assess";
    }
}

